package com.cn.wanandroidmvmm.http

import com.cn.wanandroidmvmm.globalvar.Global
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

object ApiMgr {
    val Api by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
        buildRetrofit().create(Api::class.java)
    }


    private fun buildRetrofit() =
        Retrofit.Builder()
            .apply {
                baseUrl(Global.HOST)
                client(buildOKHttp())
                addConverterFactory(GsonConverterFactory.create())
            }
            .build()


    private fun buildOKHttp() =
        OkHttpClient.Builder().apply {
            connectTimeout(30, TimeUnit.SECONDS)
            readTimeout(30, TimeUnit.SECONDS)
            writeTimeout(30, TimeUnit.SECONDS)
        }.build()

}